Algorithm String Hash
Meow King March 15, 2024 Updated: March 15, 2024 #algorithm #hash #string hashRelated Question: 841. 字符串哈希
My initial code (wrong):
using namespace std;
typedef unsigned long long ULL;
const int N = 1e5 + 10;
char str;
ULL h, p, P = 131;
ULL
int
The correct version:
ULL
and
h= h * P + str;
According to ChatGPT:
my original version:
While this does give a difference that is influenced by the characters within the substring, it does not correctly account for the position of the substring within the larger string. This means that substrings with the same characters but starting at different positions in the string could potentially have different hash values because the power of P associated with each character's position isn't correctly normalized.